/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaCadastroUsuario.java
 *
 * Created on 17/04/2011, 02:30:33
 */
package gui;

import java.util.List;
import javax.swing.JOptionPane;
import modelo.Usuario;

/**
 *
 * @author emersonmx
 */
public class JanelaCadastroUsuario extends javax.swing.JDialog implements UI {

  /** Creates new form JanelaCadastroUsuario */
  public JanelaCadastroUsuario(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    setLocationRelativeTo(null);
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        rotuloNome = new javax.swing.JLabel();
        rotuloUsuario = new javax.swing.JLabel();
        rotuloSenhaAtual = new javax.swing.JLabel();
        rotuloNovaSenha = new javax.swing.JLabel();
        rotuloRepetirSenha = new javax.swing.JLabel();
        campoNome = new javax.swing.JTextField();
        campoUsuario = new javax.swing.JTextField();
        botaoCancelar = new javax.swing.JButton();
        botaoOK = new javax.swing.JButton();
        campoSenhaAtual = new javax.swing.JPasswordField();
        campoNovaSenha = new javax.swing.JPasswordField();
        campoRepetirSenha = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gerenciamento de Usuário");

        rotuloNome.setText("Nome");

        rotuloUsuario.setText("Usuário");

        rotuloSenhaAtual.setText("Senha atual");

        rotuloNovaSenha.setText("Nova Senha");

        rotuloRepetirSenha.setText("Repetir nova senha");

        botaoCancelar.setText("Cancelar");
        botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCancelarActionPerformed(evt);
            }
        });

        botaoOK.setText("OK");
        botaoOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rotuloRepetirSenha, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(rotuloNovaSenha, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(rotuloSenhaAtual, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(rotuloUsuario, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(rotuloNome, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(campoNome, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                            .addComponent(campoUsuario, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                            .addComponent(campoSenhaAtual, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                            .addComponent(campoNovaSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                            .addComponent(campoRepetirSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(botaoOK)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botaoCancelar)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botaoCancelar, botaoOK});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rotuloNome)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rotuloUsuario)
                    .addComponent(campoUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rotuloSenhaAtual)
                    .addComponent(campoSenhaAtual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rotuloNovaSenha)
                    .addComponent(campoNovaSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rotuloRepetirSenha)
                    .addComponent(campoRepetirSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoCancelar)
                    .addComponent(botaoOK))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCancelarActionPerformed
      this.setVisible(false);
      UsuarioContainer uc = new UsuarioContainer();
      uc.setUsuario(new Usuario());
      mostrarObjeto(uc);
    }//GEN-LAST:event_botaoCancelarActionPerformed

    private void botaoOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoOKActionPerformed
      Usuario u = usuarioContainer.getUsuario(); // isso é por causa do id =D
      u.setNome(campoNome.getText());
      u.setUsuario(campoUsuario.getText());
      u.setSenha(String.valueOf(campoSenhaAtual.getPassword()));
      usuarioContainer.setUsuario(u);
      usuarioContainer.setNovaSenha(String.valueOf(campoNovaSenha.getPassword()));
      usuarioContainer.setRepetirNovaSenha(String.valueOf(campoRepetirSenha.getPassword()));

      controlador.alterar(usuarioContainer);
    }//GEN-LAST:event_botaoOKActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoCancelar;
    private javax.swing.JButton botaoOK;
    private javax.swing.JTextField campoNome;
    private javax.swing.JPasswordField campoNovaSenha;
    private javax.swing.JPasswordField campoRepetirSenha;
    private javax.swing.JPasswordField campoSenhaAtual;
    private javax.swing.JTextField campoUsuario;
    private javax.swing.JLabel rotuloNome;
    private javax.swing.JLabel rotuloNovaSenha;
    private javax.swing.JLabel rotuloRepetirSenha;
    private javax.swing.JLabel rotuloSenhaAtual;
    private javax.swing.JLabel rotuloUsuario;
    // End of variables declaration//GEN-END:variables
  Controlador controlador;
  UsuarioContainer usuarioContainer = null;

  @Override
  public Controlador getControlador() {
    return controlador;
  }

  @Override
  public void setControlador(Controlador c) {
    controlador = c;
  }

  @Override
  public void exibirMensagem(String mensagem) {
    JOptionPane.showMessageDialog(this, mensagem);
  }

  @Override
  public void exibirMensagemErro(String mensagem) {
    JOptionPane.showMessageDialog(this, mensagem, "Erro!", JOptionPane.ERROR_MESSAGE);
  }

  @Override
  public void mostrarObjeto(Object o) {
    usuarioContainer = (UsuarioContainer) o;

    Usuario usuario = usuarioContainer.getUsuario();

    campoNome.setText(usuario.getNome());
    campoSenhaAtual.setText("");
    campoUsuario.setText(usuario.getUsuario());
    campoNovaSenha.setText(usuarioContainer.getNovaSenha());
    campoRepetirSenha.setText(usuarioContainer.getRepetirNovaSenha());
  }

  @Override
  public void mostrarObjetos(List l) {
    throw new UnsupportedOperationException("Not supported yet.");
  }
}
